﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Conysoft;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.ApplicationBlocks.Data;
using System.Web.Security;

namespace VideoWorld
{
    /// <summary>
    ///		说明:更新用户详细信息
    /// </summary>
    public class Detail : System.Web.UI.Page
    {
        DataAccess.Users users = new DataAccess.Users(Global.ConnectionString);
        protected System.Web.UI.WebControls.Button Login;
        protected System.Web.UI.HtmlControls.HtmlInputButton submitUpdate;
        protected System.Web.UI.WebControls.Button buttonGoIndex;
        protected System.Web.UI.HtmlControls.HtmlInputText nickName;
        protected System.Web.UI.HtmlControls.HtmlInputText email;
        protected System.Web.UI.HtmlControls.HtmlInputText realName;
        protected System.Web.UI.HtmlControls.HtmlSelect gender;
        protected System.Web.UI.HtmlControls.HtmlSelect birthDayYYYY;
        protected System.Web.UI.HtmlControls.HtmlSelect birthDayMM;
        protected System.Web.UI.HtmlControls.HtmlInputText mobile;
        protected System.Web.UI.HtmlControls.HtmlSelect country;
        protected System.Web.UI.HtmlControls.HtmlSelect province;
        protected System.Web.UI.HtmlControls.HtmlInputText city;
        protected System.Web.UI.HtmlControls.HtmlInputCheckBox acceptEmail;
        protected System.Web.UI.HtmlControls.HtmlInputCheckBox acceptNote;
        protected System.Web.UI.WebControls.Label labelMessage;
        protected System.Web.UI.HtmlControls.HtmlInputText password;
        protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
        protected System.Web.UI.HtmlControls.HtmlInputCheckBox publishEmail;

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userName"] == null)
                {
                    Page.Response.Redirect("login.aspx");
                }
                else
                {
                    int userId = users.GetUserId(Session["userName"].ToString());
                    SqlDataReader userInfoReader = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings["ConnectionString_Pay"].ToString(), CommandType.StoredProcedure, "GetUserInfo", new SqlParameter("@ID", userId));
                    while (userInfoReader.Read())
                    {
                        nickName.Value = userInfoReader["nickName"].ToString().Trim();
                        password.Value = userInfoReader["password"].ToString().Trim();
                        email.Value = userInfoReader["email"].ToString().Trim();
                        realName.Value = userInfoReader["realName"].ToString().Trim();
                        gender.Value = userInfoReader["gender"].ToString().Trim();
                        if (userInfoReader["birthDay"].ToString().Length >= 4)
                        {
                            birthDayYYYY.Value = userInfoReader["birthDay"].ToString().Substring(0, 4).Trim();
                        }
                        if (userInfoReader["birthDay"].ToString().Length >= 5)
                        {
                            birthDayMM.Value = userInfoReader["birthDay"].ToString().Substring(4, 2).Trim();
                        }
                        mobile.Value = userInfoReader["mobile"].ToString().Trim();
                        country.Value = userInfoReader["country"].ToString().Trim().Trim();
                        province.Value = userInfoReader["province"].ToString().Trim();
                        city.Value = userInfoReader["city"].ToString().Trim();
                        acceptEmail.Checked = ((bool)userInfoReader["acceptEmail"]);
                        acceptNote.Checked = ((bool)userInfoReader["acceptNote"]);
                        publishEmail.Checked = ((bool)userInfoReader["publishEmail"]);
                    }
                    userInfoReader.Close();
                }
                this.DataBind();
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Login.Click += new System.EventHandler(this.Login_Click);
            this.buttonGoIndex.Click += new System.EventHandler(this.buttonGoIndex_Click);
            this.submitUpdate.ServerClick += new System.EventHandler(this.submitUpdate_ServerClick);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void submitUpdate_ServerClick(object sender, System.EventArgs e)
        {
            if (Session["userName"] != null)
            {
                int acceptEmailResult;
                int acceptNoteResult;
                int publishEmailResult;
                if (acceptEmail.Checked)
                {
                    acceptEmailResult = 1;
                }
                else
                {
                    acceptEmailResult = 0;
                }

                if (acceptNote.Checked)
                {
                    acceptNoteResult = 1;
                }
                else
                {
                    acceptNoteResult = 0;
                }

                if (publishEmail.Checked)
                {
                    publishEmailResult = 1;
                }
                else
                {
                    publishEmailResult = 0;
                }

                SqlParameter[] sparam = new SqlParameter[16];
                sparam[0] = new SqlParameter("@ID", users.GetUserId(Session["userName"].ToString()));
                sparam[1] = new SqlParameter("@nickName", nickName.Value);
                sparam[2] = new SqlParameter("@email", email.Value);
                sparam[3] = new SqlParameter("@realName", realName.Value);
                sparam[4] = new SqlParameter("@gender", gender.Value);
                sparam[5] = new SqlParameter("@birthDay", birthDayYYYY.Value + birthDayMM.Value);
                sparam[6] = new SqlParameter("@mobile", mobile.Value);
                sparam[7] = new SqlParameter("@country", country.Value);
                sparam[8] = new SqlParameter("@province", province.Value);
                sparam[9] = new SqlParameter("@city", city.Value);
                sparam[10] = new SqlParameter("@acceptEmail", acceptEmailResult);
                sparam[11] = new SqlParameter("@acceptNote", acceptNoteResult);
                sparam[12] = new SqlParameter("@publishEmail", publishEmailResult);
                sparam[13] = new SqlParameter("@regTime", DateTime.Now);
                sparam[14] = new SqlParameter("@remark", "");
                sparam[12] = new SqlParameter("@Result", SqlDbType.Int);
                sparam[12].Direction = ParameterDirection.Output;
                SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings["ConnectionString_Pay"].ToString(), CommandType.StoredProcedure, "UpdateUserInfo", sparam);
                users.UnRegister(Session["username"].ToString(), password.Value.ToString());
                if (Convert.ToInt32(sparam[12].Value) == 1)
                {
                    labelMessage.Text = "更新成功";
                }
                else
                {
                    labelMessage.Text = "数据库忙,更新没有成功";
                }
            }
        }

        private void Login_Click(object sender, System.EventArgs e)
        {
            Page.Response.Redirect("Videos/main.aspx");
        }

        private void buttonGoIndex_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}

